package com.qining618.cms.ucenter.controller.app;

import com.qining618.cms.ucenter.controller.app.request.AppUserBindPhoneRequest;
import com.qining618.cms.ucenter.service.UserService;
import com.qining618.common.annotation.Access;
import com.qining618.common.contants.Group;
import com.qining618.common.contants.HeaderConstants;
import com.qining618.common.context.ContextHandle;
import com.qining618.common.enums.CallSourceEnum;
import com.qining618.common.result.ApiResult;
import com.qining618.common.utils.ValidatordUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Description;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 用户 前端控制器
 * </p>
 *
 * @author qining
 * @since 2019-09-16
 */
@RestController
@RequestMapping("/app/api/ucenter/user")
public class AppUserController {

    @Autowired
    private UserService userService;

    @Access
    @PostMapping(value = "bindPhone")
    @Description("绑定手机号")
    public ApiResult bindPhone(
            HttpServletRequest request,
            @RequestBody @Validated AppUserBindPhoneRequest bindPhoneRequest) {
        CallSourceEnum callSource = CallSourceEnum.getCallSource(request.getHeader(HeaderConstants.CALL_SOURCE));

        Long userId = ContextHandle.getUserId();

        String mobile = null;
        if (CallSourceEnum.WECHAT_MINI == callSource) {
            // 参数检验
            ValidatordUtil.validate(bindPhoneRequest, Group.WECHAT_MINI.class);
            // 绑定手机号
            mobile = userService.bindWechatMiniPhone(userId, bindPhoneRequest.getCode(), bindPhoneRequest.getEncryptedData(),
                    bindPhoneRequest.getIv());
        }

        return ApiResult.ok(mobile);
    }

}

